#include "sgu.h"

SGU_NS_BEGIN

int SGUlib::_s_single = 0;
const char * SGUlib::_s_timestamp = APPLY(STRINGIZE,SGU_TIMESTAMP);
const int SGUlib::_s_version = SGU_VERSION;
const char * SGUlib::_s_release = APPLY(STRINGIZE,SGU_RELEASE);

#ifdef SGU_LINUX
const char * SGUlib::_s_platform = "Linux";
#endif

#ifdef SGU_CYGWIN
const char * SGUlib::_s_platform = "Cygwin";
#endif

#ifdef SGU_FREEBSD
const char * SGUlib::_s_platform = "FreeBSD";
#endif



void SGUlib::init(const char * name)
{
	openlog(name,LOG_CONS|LOG_PERROR|LOG_PID,LOG_USER);
}

void SGUlib::term()
{
	closelog();
}


bool SGUlib::isDebug()
{
	#ifdef SGU_DEBUG
	return true;
	#else
	return false;
	#endif
}

const char * SGUlib::queryTimeStamp()
{
	return _s_timestamp;
}

int SGUlib::queryVersion()
{
	return _s_version;
}

const char * SGUlib::queryRelease()
{
	return _s_release;
}

const char * SGUlib::queryPlatform()
{
	return _s_platform;
}


bool SGUlib::isDynamic()
{
	return SGU_isdynamic();
}

std::string SGUlib::queryBanner()
{
	char str[256];
	snprintf(str,DIM(str),"SGU %d.%s (%s) %s %s %s",
			SGUlib::queryVersion(),
	 		SGUlib::queryRelease(),
			SGUlib::queryTimeStamp(),
			SGUlib::isDynamic() ? "DYN" : "STA",
			SGUlib::isDebug() ? "DBG" : "RLS",
			SGUlib::queryPlatform()
			);
	return std::string(str);
}

const char * SGUlib::queryOwn()
{
	return "\033[1;31mz\033[1;33me\033[1;32ml\033[1;36md\033[1;34ma\033[1;35mn\033[0;39m";
}

void logerr(int fd, int error, const char * scope)
{
	std::string s = (std::string)(Format("SGU Error: function=\"%$\" fd=%$ error=\"%$\" (%$)")
		% scope % itod(fd) % strerror(error) % itod(error));
	syslog(LOG_ERR,s.data());
}
		


SGU_NS_END
